package com.codegenerate.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codegenerate.demo.config.R;
import com.codegenerate.demo.dto.AppFunDto;
import com.codegenerate.demo.entity.AppFun;
import com.codegenerate.demo.service.impl.AppFunServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/app_fun")
public class AppFunController {

    @Autowired
    AppFunServiceImpl appFunService;


    @PostMapping("/getlist")
    R<List<AppFun>> list(@RequestBody AppFunDto appFunDto){
        Page<AppFun> page = appFunService.page(
                appFunDto.buildPage(),appFunDto.dto2entity().getQueryAll()
        );
        var list=page.getRecords();
        for (int i = 0; i <list.size() ; i++) {
            var one = list.get(i);
            one.setHaveNext(appFunService.exitHaveNext(one.getId()));
        }
        return R.ok(list)
                .setCount(page.getTotal());
    }

    @PostMapping("/getlistAll")
    R<List<AppFun>> getlistAll(){
        return R.ok(appFunService.getListAll(0L));
    }

    @GetMapping("/getItem")
    R<AppFun> getItem(int id) {
        return R.ok(appFunService.getById(id));
    }

    @PostMapping("/add")
    R<Boolean> add(@RequestBody  AppFunDto appFunDto){
        var entity=appFunDto.dto2entity();
        //设置 默认
        AppFun.setDefaultValue(entity);
        return R.ok(appFunService.save(entity));
    }


    @PostMapping("/update")
    R<Integer> update(@RequestBody  AppFunDto appFunDto){
        appFunDto.validateUpdate();
        return R.ok(appFunService.getBaseMapper().update(null, appFunDto.dto2entity().getUpdateWrapper()));
    }

    @GetMapping("/del")
    R<Boolean> del(@RequestParam("id") int id) {
        return R.ok(appFunService.removeById(id));
    }
}




